package com.hyacinth.core.sysmgr.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hyacinth.common.constant.SysConstant;
import com.hyacinth.common.framework.ControllerSupport;
import com.hyacinth.common.framework.i.IServiceSupport;
import com.hyacinth.core.sysmgr.domain.SysUser;
import com.hyacinth.core.sysmgr.service.IUserService;

/**
 * 此类用于用户的操作
 * @author liujianqiu
 *
 */
@Controller
@RequestMapping("/system/user/*")
public class UserController extends ControllerSupport<SysUser> {

	private static final long serialVersionUID = 8966816439009559303L;
	
	private IUserService userService;
	
	@RequestMapping("login.do")
	public String login(@ModelAttribute SysUser user, HttpServletRequest request) throws Exception {
		HttpSession session = request.getSession();
		ServletContext application = session.getServletContext();
		SysUser _user = userService.login(user);
		if(_user!=null) {
			UUID uuid = UUID.randomUUID();
			Map<String, Object> appMap = new HashMap<String, Object>();
			appMap.put(uuid.toString(), _user);
			application.setAttribute(SysConstant.SYS_APPLICATION_USER, appMap);
			session.setAttribute(SysConstant.SYS_SESSION_USER, _user);
			session.setAttribute(SysConstant.SYS_SESSION_UUID, uuid.toString());
			return "redirect:/system/home.do";
		}
		return "redirect:/system/index.do";
	}
	
	@RequestMapping("logout.do")
	public String logout() throws Exception {
		return "";
	}
	
	@RequestMapping("enter.do")
	public String enter() throws Exception {
		return "/system/user/list";
	}

	public IUserService getUserService() {
		return userService;
	}

	@Resource
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@Override
	public IServiceSupport<SysUser> getService() {
		return this.userService;
	}

	@Override
	public String[] getFields() {
		String[] fields = {"username","password","id"};
		return fields;
	}

}
